﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFCoreRepository.DbContexts;
using EFCoreRepository.Extensions;
using EFCoreRepository.Repositories;
using Zoomtel.Entity.BIZ;
using Zoomtel.Persist.BIZ.Models;
using Zoomtel.Persist.Query;
using EFCoreRepository.Extensions;

namespace Zoomtel.Persist.BIZ
{
    public class ItemRepository : RepositoryAb<ItemEntity>
    {
        public ItemRepository(DefaultDbContext context) : base(context)
        {
        }

        public object GetItemIdByItemCode(string itemCode)
        {

            return DbContext.ExecuteScalar("select Itemid from t_biz_item where ItemCode=@ItemCode", new { ItemCode = itemCode });
        }
      
        public IEnumerable<ItemCatResultModel> QueryCat()
        {

            var result= DbContext.SqlQuery<ItemCatResultModel>("SELECT TOP 20 ItemCat FROM[zoomtel_agent].[dbo].[T_BIZ_ITEM] where itemCat is not null group by ItemCat",null);
            return result;
        }

        public async Task<int> delete(string itemId)
        {
            var result = DbContext.ExecuteSql("update [zoomtel_agent].[dbo].[T_BIZ_ITEM] set status='20',modifiedtime=GETDATE() where itemId=@itemId", new { itemId = itemId });
            return result;
        }
            public async Task<QueryResultModel<ItemEntity>> Query(ItemQueryModel model)
        {
            var paging = model.Paging();
            var baseTypeInfo = DbContext.Set<Entity.BaseType.BaseTypeInfoEntity>();
            var query = dbSet.AsQueryable();
            query = from item in dbSet
                    join basetype in baseTypeInfo on item.PlatForm equals basetype.Id
                    select new ItemEntity {
                        ClickCount=item.ClickCount,
                        ItemCat=item.ItemCat,
                        QsCount=item.QsCount,
                        ItemRemark=item.ItemRemark,
                        IsTop=item.IsTop,
                        ItemId=item.ItemId,
                        ItemCode= item.ItemCode,
                        ItemName=item.ItemName,
                        ItemPrice=item.ItemPrice,
                        ItemUrl=item.ItemUrl,
                        PicUrl=item.PicUrl,
                        Status=item.Status,
                        PlatForm=basetype.TypeContent,
                        ADCode=item.ADCode,
                        ADName=item.ADName,
                        ADFactory=item.ADFactory,
                        ADUrl=item.ADUrl,
                        CreatedTime=item.CreatedTime,
                        CreatedBy=item.CreatedBy,
                        Creator=item.Creator,
                        ModifiedBy=item.ModifiedBy,
                        ModifiedTime=item.ModifiedTime,
                        Modifier=item.Modifier,
                        AppId=item.AppId,
                        AppKey=item.AppKey,
                        AppName=item.AppName
                    };
            if (model.IsTop != null )
            {
                query = query.Where(a => a.IsTop == model.IsTop);
            }
            if (model.ItemCat != null && !"".Equals(model.ItemCat))
            {
                query = query.Where(a => a.ItemCat == model.ItemCat);
            }
            if (model.Status != null && !"".Equals(model.Status)&& !"-1".Equals(model.Status))
            {
                query = query.Where(a => a.Status == model.Status);
            }
            if (model.ItemInfo != null&&!"".Equals(model.ItemInfo))
            {
                query = query.Where(a => a.ItemCode.Contains(model.ItemInfo)|| a.ItemName.Contains(model.ItemInfo));
            }
            if (model.ADInfo != null && !"".Equals(model.ADInfo))
            {
                query = query.Where(a => a.ADCode.Contains(model.ADInfo) || a.ADFactory.Contains(model.ADInfo) || a.ADName.Contains(model.ADInfo));
            }
            if (model.PlatForm != null && !"".Equals(model.PlatForm))
            {
                query = query.Where(a => a.PlatForm==model.PlatForm);
            }
            if (!paging.OrderBy.Any())
            {
                query = query.OrderByDescending(m => m.CreatedTime);
            }
            else
            {
            foreach(Sort orderItem in paging.OrderBy)
                {
                    if (orderItem.Type == SortType.Desc)
                    {
                        if(orderItem.OrderBy== "ClickCount")
                        {
                            query = query.OrderByDescending(m => m.ClickCount);

                        }
                        else
                        {
                            query = query.OrderByDescending(m => m.CreatedTime);

                        }

                    }
                    else
                    {
                        if (orderItem.OrderBy == "ClickCount")
                        {
                            query = query.OrderBy(m => m.ClickCount);

                        }
                        else
                        {
                            query = query.OrderBy(m => m.CreatedTime);

                        }

                    }
                }
            


            }
            return await query.PaginationGetResult(paging);
        }
    }
}
